package com.sunwayworld.basemodule.coredata.experimentaltemplate.service.impl;

import com.sunwayworld.basemodule.coredata.experimentaltemplate.bean.LimsExperimentalTemplateTestBean;
import com.sunwayworld.basemodule.coredata.experimentaltemplate.bean.LimsExperimentalTemplateTestMethodBean;
import com.sunwayworld.basemodule.coredata.experimentaltemplate.dao.LimsExperimentalTemplateTestDao;
import com.sunwayworld.basemodule.coredata.experimentaltemplate.service.LimsExperimentalTemplateTestService;
import com.sunwayworld.basemodule.coredata.test.bean.LimsTestMethodBean;
import com.sunwayworld.basemodule.coredata.test.service.LimsTestMethodService;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.constant.Constant;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.database.sql.Order;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import com.sunwayworld.framework.utils.CollectionUtils;
import com.sunwayworld.framework.utils.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsExperimentalTemplateTestServiceImpl implements LimsExperimentalTemplateTestService {

    @Autowired
    private LimsExperimentalTemplateTestDao limsExperimentalTemplateTestDao;

    @Autowired
    @Lazy
    private LimsTestMethodService testMethodService;

    @Autowired
    @Lazy
    private LimsExperimentalTemplateTestMethodServiceImpl etMethodService;

    @Override
    @SuppressWarnings("unchecked")
    public LimsExperimentalTemplateTestDao getDao() {
        return limsExperimentalTemplateTestDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        List<LimsExperimentalTemplateTestBean> testBeans = jsonWrapper.parse(LimsExperimentalTemplateTestBean.class);
        LimsExperimentalTemplateTestBean searchBean = new LimsExperimentalTemplateTestBean();
        searchBean.setEtId(testBeans.get(0).getEtId());
        LimsExperimentalTemplateTestBean bean = getDao().selectFirstIfPresent(searchBean, Order.desc("orderNo"));
        Long orderNo = ObjectUtils.isEmpty(bean) ? 1L : bean.getOrderNo() + 1;
        for (LimsExperimentalTemplateTestBean testBean : testBeans) {
            testBean.setId(ApplicationContextHelper.getNextIdentity());
            testBean.setOrderNo(orderNo);
            orderNo ++;
        }
        List<Long> testIdList = testBeans.stream().map(LimsExperimentalTemplateTestBean::getTestId).collect(Collectors.toList());
        //同步工序方法
        List<LimsTestMethodBean> testMethodList = testMethodService.selectListByFilter(SearchFilter.instance()
                .match("TESTID", testIdList).filter(MatchPattern.OR).match("ACTIVATEDFLAG", Constant.ACTIVATED_STATUS_YES).filter(MatchPattern.EQ));
        if(!CollectionUtils.isEmpty(testMethodList)){
            List<LimsExperimentalTemplateTestMethodBean> insertEtMethodList = new ArrayList<>();
            for (LimsExperimentalTemplateTestBean testBean : testBeans) {
                Iterator<LimsTestMethodBean> iterator = testMethodList.iterator();
                while (iterator.hasNext()) {
                    LimsTestMethodBean testMethodBean = iterator.next();
                    if (testMethodBean.getTestId().equals(testBean.getTestId())) {
                        LimsExperimentalTemplateTestMethodBean spMethod = new LimsExperimentalTemplateTestMethodBean();
                        spMethod.setId(ApplicationContextHelper.getNextIdentity());
                        spMethod.setEtId(testBean.getEtId());
                        spMethod.setEtTestId(testBean.getId());
                        spMethod.setTestMethodId(testMethodBean.getId());
                        spMethod.setMethodCode(testMethodBean.getExt$Item("methodcode"));
                        spMethod.setMethodDesc(testMethodBean.getExt$Item("methoddesc"));
                        spMethod.setDefaultMethod(testMethodBean.getDefaultMethod());
                        insertEtMethodList.add(spMethod);
                        iterator.remove();
                    }
                }
            }
            etMethodService.getDao().insert(insertEtMethodList);
        }
        getDao().insert(testBeans);
        return testBeans.get(0).getId();
    }

    @Override
    @Transactional
    public void delete(RestJsonWrapperBean wrapper) {
        List<LimsExperimentalTemplateTestBean> parse = wrapper.parse(LimsExperimentalTemplateTestBean.class);
        if(CollectionUtils.isEmpty(parse)) return;
        // 级联删除模板工序方法
        List<LimsExperimentalTemplateTestMethodBean> testMethodBeans = new ArrayList<>();
        parse.forEach(e -> {
            LimsExperimentalTemplateTestMethodBean methodBean = new LimsExperimentalTemplateTestMethodBean();
            methodBean.setEtTestId(e.getId());
            testMethodBeans.add(methodBean);
        });
        etMethodService.getDao().deleteBy(testMethodBeans, "etTestId");
        getDao().deleteBy(parse);
    }
}
